package com.edu.shiro.controller;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class LoginController {
	
	@GetMapping("/login")
	public String showLogin(){
		return "loginpage";
	}
	
	@PostMapping("/login")
	public String login(String username, String password,boolean rememberMe,Model model){
		UsernamePasswordToken token = new UsernamePasswordToken(username,password,rememberMe);
		Subject subject = SecurityUtils.getSubject();
		try{
			subject.login(token);
			return "index";
		}catch (UnknownAccountException e) {
			model.addAttribute("msg", "用户名不存在");
		}catch(IncorrectCredentialsException e){
			model.addAttribute("msg","密码错误");
		}catch(LockedAccountException e){
			model.addAttribute("msg","用户被锁定");
		}
		return "loginpage";	
	}
}
